package org.dftproject.genesis.ui.pages;

import org.dftproject.genesis.ui.Activator;
import org.dftproject.genesis.ui.figures.HyperlinkFigure;
import org.dftproject.genesis.ui.figures.IHyperlinkListener;
import org.dftproject.genesis.ui.figures.PageContainer;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.draw2d.Figure;
import org.eclipse.draw2d.FigureCanvas;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.MarginBorder;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PlatformUI;

public class AddCollectionPage extends AbstractPagePart {

	public static final String ID = AddCollectionPage.class.getName();
	
	private FigureCanvas canvas;
	
	public void init(IPageSite site, IPageInput input) throws PageInitException {
		this.setSite(site);
		this.setInput(input);
	}

	@Override
	public void createPartControl(Composite parent) {		
		createContents(parent);
	}
	
	protected void createContents(Composite parent) {
		// Create the main Draw2D canvas

		canvas = new FigureCanvas(parent);

		// Create the root figure

		Figure topContainer = new PageContainer();
		canvas.getViewport().setContentsTracksWidth(true);
		canvas.setContents(topContainer);
		
		topContainer.add(PageUtils.createH1Label("Open a file"));
		
		topContainer.add(PageUtils.createFlowedText("Genesis can open the following types of files:"));
		
		IFigure links = new Figure();
		ToolbarLayout layout = new ToolbarLayout(false);
		layout.setSpacing(10);
		layout.setStretchMinorAxis(false);
		links.setLayoutManager(layout);
		links.setBorder(new MarginBorder(10, 10, 10, 0));
		topContainer.add(links);
		
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint extensionPoint = registry.getExtensionPoint(Activator.ID, Activator.ADD_COLLECTION_PAGES_EXTENSION_POINT);
		IExtension[] extensions = extensionPoint.getExtensions();
		for (int i = 0; i < extensions.length; i++) {
			IConfigurationElement[] configElements = extensions[i].getConfigurationElements();
			for (int j = 0; j < configElements.length; j++) {
				final String name = configElements[j].getAttribute("name");
				final String pageId = configElements[j].getAttribute("id");
				
				HyperlinkFigure addHyperlink = new HyperlinkFigure(name);
				addHyperlink.addHyperlinkListener(new IHyperlinkListener() {

					public void linkActivated(Object data, int stateMask) {
						IWorkbench workbench = PlatformUI.getWorkbench();
						IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
						IWorkbenchPage workbenchPage = window.getActivePage();
						PageManager.getDefault().openPage(workbenchPage, new AbstractPageInput() {
							
							{
								setLabel(name);
								setImageDescriptor(Activator.getImageDescriptor("icons/folder_page.png"));
							}
							
						}, pageId, true, (stateMask & SWT.CTRL) != 0);
					}
					
				});
				links.add(addHyperlink);
			}
		}
	}

	@Override
	public void dispose() {
	}

	@Override
	public void setFocus() {
		canvas.setFocus();
	}

}
